package com.example.hangzhou.welbeing.mytestdemoapp.fragment;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hangzhou.welbeing.mytestdemoapp.R;
import com.example.hangzhou.welbeing.mytestdemoapp.activity.RecyclerViewActivity;
import com.example.hangzhou.welbeing.mytestdemoapp.animation.animationActivity.AnimatedSpreadActivity;
import com.example.hangzhou.welbeing.mytestdemoapp.animation.animationActivity.AnimatedTransitionsDetailsPageSlideActivity;
import com.example.hangzhou.welbeing.mytestdemoapp.animation.animationActivity.AnimatedTransitionsExplodeAndPropagationActivity;
import com.example.hangzhou.welbeing.mytestdemoapp.animation.animationAdapter.AnimatedTransitionsfiveAdapter;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 杭州惟君医疗科技有限公司14
 * Created by 20433 on 2018/5/14
 *
 * @desc 东风山河绿，春已不折柳，繁花看不尽，处处惹人留
 * @desc 佛祖镇楼，bug辟容易
 */

public class MyHomePagerFragment extends Fragment {
    Context mcontext;

    @Bind(R.id.fragment_my_home_pager_recycler_view)
    RecyclerView fragmentMyHomePagerRecyclerView;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mcontext = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_home_pager, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mcontext, LinearLayoutManager.VERTICAL, false);
        // 设置布局管理器
        fragmentMyHomePagerRecyclerView.setLayoutManager(mLayoutManager);
        AnimatedTransitionsfiveAdapter animatedTransitionsfiveAdapter = new AnimatedTransitionsfiveAdapter();
        // 设置adapter
        fragmentMyHomePagerRecyclerView.setAdapter(animatedTransitionsfiveAdapter);
        animatedTransitionsfiveAdapter.setOnItemClickListener(new AnimatedTransitionsfiveAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view) {
                int position = fragmentMyHomePagerRecyclerView.getChildAdapterPosition(view);
                PageJump(position, view);
            }

            @Override
            public void onItemClickFirst(View view) {

            }
        });
    }

    private void PageJump(int position, View view) {

        switch (position) {
            case 0:
                Intent intent = new Intent(mcontext, AnimatedTransitionsExplodeAndPropagationActivity.class);
                startActivity(intent);
                break;
            case 1:
                Intent intent_02 = new Intent(mcontext, AnimatedTransitionsDetailsPageSlideActivity.class);
                startActivity(intent_02, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
            case 2:
                Intent intent_05 = new Intent(mcontext, AnimatedSpreadActivity.class);
                startActivity(intent_05, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
            case 3:
                Intent intent_06 = new Intent(mcontext, RecyclerViewActivity.class);
                startActivity(intent_06, ActivityOptions.makeSceneTransitionAnimation(getActivity(), view, "shareNames").toBundle());
                break;
            case 4:
                snackbarShow(view);
                break;
            case 5:

                break;
            case 6:

                break;
            case 7:
                break;
            case 8:
                break;
            case 9:
                break;

        }
    }

    private void snackbarShow(View view) {
        Snackbar snackbar = Snackbar.make(view, "已删除一个会话", Snackbar.LENGTH_SHORT)
                .setAction("撤销", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Toast.makeText(mcontext, "撤销了删除", Toast.LENGTH_SHORT).show();

                    }
                })
                .setCallback(new Snackbar.Callback() {
                    @Override
                    public void onDismissed(Snackbar snackbar, int event) {
                        super.onDismissed(snackbar, event);

                        switch (event) {

                            case Snackbar.Callback.DISMISS_EVENT_CONSECUTIVE:
                            case Snackbar.Callback.DISMISS_EVENT_MANUAL:
                            case Snackbar.Callback.DISMISS_EVENT_SWIPE:
                            case Snackbar.Callback.DISMISS_EVENT_TIMEOUT:
                                //TODO 网络操作
                              //  Toast.makeText(mcontext, "删除成功", Toast.LENGTH_SHORT).show();
                                break;
                            case Snackbar.Callback.DISMISS_EVENT_ACTION:
                                Toast.makeText(mcontext, "撤销了删除操作", Toast.LENGTH_SHORT).show();
                                break;

                        }

                    }

                    @Override
                    public void onShown(Snackbar snackbar) {
                        super.onShown(snackbar);
                        Toast.makeText(mcontext, "Snackbar show", Toast.LENGTH_SHORT).show();
                    }
                });
        customStyleSnackbar(snackbar);//调用自定义SnackBar方法
        snackbar.show();
    }

    private void customStyleSnackbar(Snackbar snackbar) {

        LinearLayout snackbarView = (LinearLayout)snackbar.getView();//获取SnackBar布局View实例
        TextView textView = (TextView) snackbarView.findViewById(R.id.snackbar_text);//获取文本View实例
        Button button = (Button)snackbarView.findViewById(R.id.snackbar_action);//获取按钮View实例

        snackbarView.setBackgroundColor(Color.parseColor("#708090"));
        button.setTextColor(Color.parseColor("#f44336"));
        textView.setTextColor(Color.parseColor("#ffffff"));
        textView.setTextSize(16);
        button.setTextSize(16);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
